package com.bcntouch.touch2act.Opine;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.Data.DBHelper;

public class OpinionStorage {

	public static final String 	URI_PATH 			= "opinion";
	public static final Uri    	CONTENT_URI 		= Uri.parse(Constants.CONTENT_SCHEME + Constants.AUTHORITY + "/" + URI_PATH);
	
	public static final class OpinionMetaData implements BaseColumns {
			public static final String TABLE_NAME 					= URI_PATH;
			
			public static final String QUESTION						= "question";
			public static final String CATEGORY						= "category";
			public static final String NUM_VOTES					= "numVotes";
			public static final String NUM_POSITIVE_VOTES			= "numPositiveVotes";
			public static final String NUM_NEGATIVE_VOTES			= "numNegativeVotes";
			public static final String HAVE_VOTED					= "haveVoted";
			public static final String YOUR_VOTE					= "yourVote";
			public static final String IS_YOURS						= "isYours";
	}
	
	// These are the Opinion columns that we will retrieve.
    static final String[] OPINIONS_SUMMARY_PROJECTION = new String[] {
    	OpinionMetaData._ID,
    	OpinionMetaData.QUESTION,
    	OpinionMetaData.CATEGORY,
    	OpinionMetaData.NUM_VOTES,
    	OpinionMetaData.NUM_POSITIVE_VOTES,
    	OpinionMetaData.NUM_NEGATIVE_VOTES,
    	OpinionMetaData.HAVE_VOTED,
    	OpinionMetaData.YOUR_VOTE,
    	OpinionMetaData.IS_YOURS
    };
	
	public static void initStorage(SQLiteDatabase db) {
		// Create the Products Table
		try {
			db.execSQL( "CREATE TABLE " + OpinionMetaData.TABLE_NAME + " ("
						+ OpinionMetaData._ID 						+ " INTEGER PRIMARY KEY,"
						+ OpinionMetaData.QUESTION 					+ " TEXT,"
						+ OpinionMetaData.CATEGORY       			+ " TEXT,"
						+ OpinionMetaData.NUM_VOTES       			+ " INTEGER,"
						+ OpinionMetaData.NUM_POSITIVE_VOTES    	+ " INTEGER,"
						+ OpinionMetaData.NUM_NEGATIVE_VOTES       	+ " INTEGER,"
						+ OpinionMetaData.HAVE_VOTED		       	+ " INTEGER,"
						+ OpinionMetaData.YOUR_VOTE       			+ " INTEGER,"
						+ OpinionMetaData.IS_YOURS       			+ " INTEGER"
						+ ");"
					);
		}
		catch ( Exception e ) {
		}
	}
	
	public static void insertOpinion(Context context, Opinion opinion) {
		if (opinion == null)
			return;
		
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put(OpinionMetaData.QUESTION, opinion.getQuestion());
		values.put(OpinionMetaData.CATEGORY, opinion.getCategory());
		values.put(OpinionMetaData.NUM_VOTES, opinion.getNumVotes());
		values.put(OpinionMetaData.NUM_POSITIVE_VOTES, opinion.getNumPositiveVotes());
		values.put(OpinionMetaData.NUM_NEGATIVE_VOTES, opinion.getNumNegativeVotes());
		values.put(OpinionMetaData.HAVE_VOTED, opinion.getHaveVoted());
		values.put(OpinionMetaData.NUM_VOTES, opinion.getNumVotes());
		values.put(OpinionMetaData.IS_YOURS, opinion.getIsYours());
		
		db.insert( OpinionMetaData.TABLE_NAME, null, values);
	}
	
	public static void updateOpinion(Context context, Opinion opinion) {
		if (opinion == null || opinion.getId() == null)
			return;
		
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getWritableDatabase();
		
		String whereClause = OpinionMetaData._ID + "=" + opinion.getId();
		
		ContentValues values = new ContentValues();
		
		values.put(OpinionMetaData.QUESTION, opinion.getQuestion());
		values.put(OpinionMetaData.CATEGORY, opinion.getCategory());
		values.put(OpinionMetaData.NUM_VOTES, opinion.getNumVotes());
		values.put(OpinionMetaData.NUM_POSITIVE_VOTES, opinion.getNumPositiveVotes());
		values.put(OpinionMetaData.NUM_NEGATIVE_VOTES, opinion.getNumNegativeVotes());
		values.put(OpinionMetaData.HAVE_VOTED, opinion.getHaveVoted());
		values.put(OpinionMetaData.NUM_VOTES, opinion.getNumVotes());
		values.put(OpinionMetaData.IS_YOURS, opinion.getIsYours());
		
		db.update(OpinionMetaData.TABLE_NAME, values, whereClause, null);
	}
	
	public static Loader<Cursor> getAllOpinionsCursorLoader(Context context) {
		Uri baseUri = CONTENT_URI;
        
		String order = OpinionMetaData.NUM_VOTES + " DESC";
		
		CursorLoader cursorLoader = new CursorLoader(context, baseUri, OPINIONS_SUMMARY_PROJECTION, null, null, order);
		
        return cursorLoader;
	}
	
	public static Opinion getOpinionById(Context context, String opinionId) {
		if (opinionId == null)
			return null;
		
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getReadableDatabase();
		
		Opinion ret = null;
		
		String selection = OpinionMetaData._ID + "=" + opinionId;
		Cursor cur = db.query( OpinionMetaData.TABLE_NAME, OPINIONS_SUMMARY_PROJECTION, selection, null, null, null, null);
		
		if ( cur.moveToFirst() ) {
			String id 				= cur.getString(cur.getColumnIndex(OpinionMetaData._ID));
			String question 		= cur.getString(cur.getColumnIndex(OpinionMetaData.QUESTION));
			String category 		= cur.getString(cur.getColumnIndex(OpinionMetaData.CATEGORY));
			int numVotes 			= cur.getInt(cur.getColumnIndex(OpinionMetaData.NUM_VOTES));
			int numPositiveVotes 	= cur.getInt(cur.getColumnIndex(OpinionMetaData.NUM_POSITIVE_VOTES));
			int numNegativeVotes 	= cur.getInt(cur.getColumnIndex(OpinionMetaData.NUM_NEGATIVE_VOTES));
			int haveVoted 			= cur.getInt(cur.getColumnIndex(OpinionMetaData.HAVE_VOTED));
			int yourVote 			= cur.getInt(cur.getColumnIndex(OpinionMetaData.YOUR_VOTE));
			int isYours				= cur.getInt(cur.getColumnIndex(OpinionMetaData.IS_YOURS));
			
			cur.close();
			ret = new Opinion(id,
							  question,
							  category,
							  numVotes,
							  numPositiveVotes,
							  numNegativeVotes,
							  haveVoted,
							  yourVote,
							  isYours);
		}
		
		return ret;
	}
	
	public static void initDefaultOpinions(Context context) {
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getWritableDatabase();
		
		Cursor cur = db.query( OpinionMetaData.TABLE_NAME, OPINIONS_SUMMARY_PROJECTION, null, null, null, null, null);
		
		if ( !cur.moveToFirst() ) {
			Opinion[] defaultOpinions = Opinions.getOpinionsArray(context);
			for (Opinion opinion : defaultOpinions) {
				insertOpinion(context, opinion);
			}
		}
	}
	
}
